/**
 * 
 */

package com.chengyu.eyc.aop;

import javax.servlet.http.HttpServletResponse;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.chengyu.eyc.base.ResponseResult;
import com.chengyu.eyc.enums.PayStatusEnum;
import com.chengyu.eyc.model.EycOrder;
import com.chengyu.eyc.queue.RabbitMQConfig;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author kevin
 *
 */
@Slf4j
@Component
@Aspect
public class EycOrderAspect {

    @Autowired
    RabbitTemplate rabbitTemplate; // 使用RabbitTemplate,这提供了接收/发送等等方法

    @AfterReturning(returning = "obj", pointcut = "execution(* com.chengyu.eyc.service.EycServiceImpl.insertOrder(..))")
    public void afterReturning(JoinPoint joinPoint, Object obj) {
        try {
            if (obj instanceof EycOrder) {
                RequestAttributes ra = RequestContextHolder.getRequestAttributes();
                ServletRequestAttributes sra = (ServletRequestAttributes)ra;
                if (sra != null) {
                    HttpServletResponse response = sra.getResponse();
                    if (response != null) {
                        EycOrder eycOrder = (EycOrder)obj;
                        log.info("订单支付完成发送消息：{}", eycOrder.getOrderNum());
                        /**
                         * 发送消息 参数一：交换机名称 参数二：路由key: order.springboot-rabbitmq,符合路由order.#规则即可 参数三：发送的消息
                         */
                        rabbitTemplate.convertAndSend(RabbitMQConfig.ORDER_TOPIC_EXCHANGE, "order." + eycOrder.getOrderNum(), eycOrder);
                    }

                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @AfterReturning(returning = "obj", pointcut = "execution(* com.chengyu.eyc.controller.WechatController.applyRefund(..))")
    public void applyRefund(JoinPoint joinPoint, Object obj) {
        try {
            if (obj instanceof ResponseResult<?>) {
                ResponseResult<?> res = (ResponseResult<?>)obj;
                if (res.getData() instanceof EycOrder) {
                    EycOrder eycOrder = (EycOrder)res.getData();
                    log.info("订单:{},支付状态：{}", eycOrder.getOrderNum(),eycOrder.getPayStatus());
                    if(StrUtil.equals(eycOrder.getPayStatus(), PayStatusEnum.REFUNDING.getCode() )) {
                        /**
                         * 发送消息 参数一：交换机名称 参数二：路由key: order.springboot-rabbitmq,符合路由order.#规则即可 参数三：发送的消息
                         */
                        rabbitTemplate.convertAndSend(RabbitMQConfig.MESSAGE_TOPIC_EXCHANGE, "message.001", String.format("用户：[%s] 订单：[%s]申请退款,请审核确认!", eycOrder.getUserId(),eycOrder.getOrderNum()));
                    }
                
                }

            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

}
